"""
Pydantic 的模型解析器
"""

from typing import (
    Dict,
    Optional,
)

from utils.validator_utils.pydantic_x.version import is_v1_model


def parse_model_fields(
    model,
    field_dic: Dict[str, str],
    parent_field_name: Optional[str] = None,
    connector: str = ".",
) -> None:
    """
    解析 Pydantic 模型字段数据

    Args:
        model: 模型（BaseModel 的子类）
        field_dic: 解析后的字段字典
        parent_field_name: 父级字段名称
        connector: 字段字典连接符

    Examples:
        >>> from devtools import debug
        >>> from pydantic import BaseModel, Field
        >>>
        >>>
        >>> class Hobby(BaseModel):
        ...     name: str = Field(..., title="名称", min_length=1, max_length=10)
        ...     years: int = Field(..., title="持续时长", gt=1, lt=100)
        ...
        >>>
        >>> class User(BaseModel):
        ...     name: str = Field(..., title="姓名", min_length=3, max_length=20)
        ...     hobbies: list[Hobby] = Field(..., title="爱好")
        >>>
        >>>
        >>> dic = {}
        >>> parse_model_fields(User, dic)
        >>> debug(dic)
        {
            "name": "姓名",
            "hobbies": "爱好",
            "hobbies.name": "爱好.名称",
            "hobbies.years": "爱好.持续时长",
        }
    """

    if is_v1_model(model):
        from utils.validator_utils.pydantic_x.parser.v1_model_parser import parse_v1_model_fields as func

    else:
        from utils.validator_utils.pydantic_x.parser.v2_model_parser import parse_v2_model_fields as func

    return func(model, field_dic, parent_field_name, connector)
